﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;

namespace Ini_Config.CustomControl
{
    internal class ToastHelper
    {
        public static async Task ShowToast(string message, string type = "ok")
        {
            // 创建 Toast 控件
            var toast = new Label
            {
                Content = message,
                Padding = new Thickness(12),
                Foreground = Brushes.White,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Bottom,
                // 设置 Toast 的大小和位置
                Width = 200,
                Height = 50,
                Margin = new Thickness(0, 0, 0, -600) // 设置底部的边距，将 Toast 定位在底部上方
            };

            // 根据类型设置边框颜色、字体颜色和背景颜色
            if (type == "ok")
            {
                toast.BorderBrush = Brushes.Green;
                toast.Foreground = Brushes.Green;
                toast.Background = Brushes.White;
            }
            else if (type == "error")
            {

                toast.BorderBrush = Brushes.Red;
                toast.Foreground = Brushes.Red;
                toast.Background = Brushes.White;
            }

            // 设置边框、字体颜色
            var style = new Style(typeof(Label));
            style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(1)));
            style.Setters.Add(new Setter(Control.FontFamilyProperty, new FontFamily("Arial")));
            //style.Setters.Add(new Setter(Control.FontSizeProperty, 12));

            // 应用样式
            toast.Style = style;

            // 创建并配置显示动画
            var fadeInAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.1)));
            var fadeOutAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.1)))
            {
                BeginTime = TimeSpan.FromSeconds(0.2)
            };
            fadeOutAnimation.Completed += (s, e) =>
            {
                // 动画完成后移除 Toast 控件
                if (toast.Parent is Panel parentPanel)
                {
                    parentPanel.Children.Remove(toast);
                }
            };

            // 添加 Toast 控件到容器中
            if (Application.Current.MainWindow is Window mainWindow)
            {
                if (mainWindow.Content is Panel mainPanel)
                {
                    mainPanel.Children.Add(toast);
                }
            }

            // 开始动画
            toast.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
            await Task.Delay(500);
            toast.BeginAnimation(UIElement.OpacityProperty, fadeOutAnimation);
        }
    }
}
